import Vue from 'vue'
import Router from 'vue-router'
// 提示框
import {
  Dialog
} from "vant";
// 登录组件
import login from '@/components/login'
//首页导航组件
import index from '@/components/index'
//首页组件
import main from '@/components/main'
//下单组件
import order from '@/components/order'
//查单组件
import search from '@/components/search'
//消息组件
import message from '@/components/message'
//我的personal组件
import personal from '@/components/personal'
//关于我们
import aboutUs from '@/components/aboutUs'
//反馈意见
import advise from '@/components/advise'
//修改密码
import updatePwd from '@/components/updatePwd'
//测试
import customer from '@/components/customer'
//选择存货
import stock from '@/components/stock'
// 固定一级路由
const path = "/app";
var _this = this;

// 解决导航栏中的vue-router在3.0版本以上重复点菜单报错问题
// 解决vue实例中beforeRouterLeave取消时间报错
const originalPush = Router.prototype.push
Router.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) {
    return originalPush.call(this, location, onResolve, onReject)
  }
  return originalPush.call(this, location).catch(err => err)
}
Vue.use(Router);
export default new Router({
  routes: [
    // 路由重定向
    {
      path: '*',
      redirect: `${path}/login`,
    },
    //选择存货
    {
      path: `${path}/stock`,
      name: 'stockPage',
      component: stock,
    },
    //测试
    {
      path: `${path}/customer`,
      name: 'customerPage',
      component: customer,
    },
    // 登录页面路由
    {
      path: `${path}/login`,
      name: 'loginPage',
      component: login,
      beforeEnter: (to, from, next) => {
        if (from.name != null) {
          if (Object.keys(to.query).length === 0) {
            Dialog.confirm({
                title: "提示",
                message: "退出系统？",
              })
              .then(() => {
                // on confirm
                next();
              })
              .catch(() => {
                // on cancel
                next(false);
              });
          }
        } else {
          next();
        }
      },
    },
    // 关于我们
    {
      path: `${path}/aboutUs`,
      name: 'aboutUsPage',
      component: aboutUs,
    },
    // 意见反馈
    {
      path: `${path}/advise`,
      name: 'advisePage',
      component: advise,
    },
    // 修改密码
    {
      path: `${path}/updatePwd`,
      name: 'updatePwdPage',
      component: updatePwd,
    },
    // 查看订单明细
    {
      path: `${path}/orderDetial`,
      name: 'orderDetialPwdPage',
      component: order,
    },
    // 首页导航页面路由
    {
      path: `${path}/index`,
      name: 'indexPage',
      component: index,
      children: [
        // 首页页面路由
        {
          path: `${path}/index/main`,
          name: 'mainPage',
          component: main
        },
        // 下单页面路由
        {
          path: `${path}/index/order`,
          name: 'orderPage',
          component: order
        },
        // 查单页面路由
        {
          path: `${path}/index/search`,
          name: 'searchPage',
          component: search
        },
        // 消息页面路由
        {
          path: `${path}/index/message`,
          name: 'messagePage',
          component: message
        },
        // 我的页面路由
        {
          path: `${path}/index/personal`,
          name: 'personalPage',
          component: personal,
        },
      ]
    },

  ],
  // 解决项目打包后刷新页面无法访问图片等资源的bug
  mode: "hash"
})
